package com.anbao.newanbao;

import com.anbao.action.ActivityManagerAction;
import com.anbao.server.SeocketServer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.Toast;
/**
 * 设置界面
 * @author tiansl
 *
 */
public class SettingActivity extends Activity implements OnClickListener{

	private RelativeLayout updateUserPwd_text;
	private RelativeLayout closeUser_text;
	private RelativeLayout updateSystem_text;
	private  RelativeLayout about_text;
	
	private RelativeLayout exitSystem_text;
	private ProgressDialog pd;
	
	private ImageView left_img;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_setting);
		iniView();
	}
	private void iniView() {
		updateUserPwd_text = (RelativeLayout) findViewById(R.id.updateUserPwd_text);
		updateUserPwd_text.setOnClickListener(this);
		closeUser_text = (RelativeLayout) findViewById(R.id.closeUser_text);
		closeUser_text.setOnClickListener(this);
		updateSystem_text = (RelativeLayout) findViewById(R.id.updateSystem_text);
		updateSystem_text.setOnClickListener(this);
		about_text = (RelativeLayout) findViewById(R.id.about_text);
		about_text.setOnClickListener(this);
		exitSystem_text = (RelativeLayout) findViewById(R.id.exitSystem_text);
		exitSystem_text.setOnClickListener(this);
		
		left_img = (ImageView) findViewById(R.id.left_img);
		left_img.setOnClickListener(this);
		
		
		
	}
	@Override
	public void onClick(View arg0) {
		
		Intent intent = null;
		switch (arg0.getId()) {
		case R.id.updateUserPwd_text:
			
			intent = new Intent(SettingActivity.this, UpdataUserPwdActivity.class);
			startActivity(intent);
			break;
	case R.id.closeUser_text:
			
			closeUser();
			break;
	case R.id.updateSystem_text:
		
		updateSystem();
		break;
	case R.id.about_text:
		
		aboutSystem();
		break;
	case R.id.left_img:
		
//		Intent intentToMainActivity = new Intent(SettingActivity.this,MainActivity.class);
//		startActivity(intentToMainActivity);
		SettingActivity.this.finish();
		
		break;
	case R.id.exitSystem_text:
		
		exitSystem();
		break;
		}
		
	}
	/**
	 * 退出系统
	 */
	private void exitSystem() {
		
		
		new AlertDialog.Builder(SettingActivity.this).setTitle("").setMessage("是否退出")  
		.setPositiveButton("是",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				
				//先将其他的界面关闭
				ActivityManagerAction.getScreenManager().popAllActivityExceptOne(SettingActivity.class);
				
				Log.e("newAnBao", "关闭界面 ");
				
				Intent intent = new Intent(SettingActivity.this, SeocketServer.class);
				stopService(intent);
				
				Log.e("newAnBao", "关闭socket服务 ");
				
				//然后关闭设置界面
				SettingActivity.this.finish();
				
				Log.e("newAnBao", "关闭设置界面 ");
				
			}
		} )  
		.setNegativeButton("否", null)  
		.show(); 
	}
	private void aboutSystem() {

		Intent intent = new Intent(SettingActivity.this,AboutActivity.class);
		startActivity(intent);
		
	}
	private void updateSystem() {

	      //构建一个下载进度条
	      pd= ProgressDialog.show(SettingActivity.this, "", "正在检查更新…");
	      
	      
	      new Thread(){
	         public void run(){
	            //在这里执行长耗时方法
	        	 try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        	 
	            //执行完毕后给handler发送一个消息
	            handler.sendEmptyMessage(0);
	         }
	      }.start();
	
		
	}
	private void closeUser() {

		
		new AlertDialog.Builder(SettingActivity.this).setTitle("").setMessage("注销当前账号")  
		.setPositiveButton("是",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				
				Toast.makeText(SettingActivity.this, "注销当前账号", 1).show();
			}
		} )  
		.setNegativeButton("否", null)  
		.show(); 
		
	}
	
	
	private Handler handler =new Handler(){
		   @Override
		   //当有消息发送出来的时候就执行Handler的这个方法
		   public void handleMessage(Message msg){
		      super.handleMessage(msg);
		      //只要执行到这里就关闭对话框
		      pd.dismiss();
		   }
		};
	

}
